package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id)
    {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        NursingPlan nursingPlan = getById(id);
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        /**
         * 查询与当前护理计划关联的所有护理项目计划
         * 使用lambda表达式构建查询条件，根据planId进行等值匹配
         */
        List<NursingProjectPlan> nursingProjectPlans = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        /**
         * 类型转换：将护理项目计划实体列表转换为视图对象列表
         * 主要字段转换包括：
         * - 将projectId转换为字符串类型
         * - 将executeCycle转换为字符串类型
         * - 将executeFrequency转换为Long类型
         */
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId() + "");
            nursingProjectPlanVo.setId(nursingProjectPlan.getId());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle() + "");
            nursingProjectPlanVo.setExecuteTime(nursingProjectPlan.getExecuteTime());
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(nursingProjectPlan.getExecuteFrequency()));
            nursingProjectPlanVo.setPlanId(nursingProjectPlan.getPlanId());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(nursingProjectPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan (NursingPlanDto nursingPlanDto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        this.save(nursingPlan);
        /**
         * 处理关联的护理项目计划
         * 如果护理计划DTO中包含护理项目计划列表，则为每个护理项目计划设置护理计划ID
         */
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
        }
        /**
         * 批量保存护理项目计划
         * 使用iNursingProjectPlanService的saveBatch方法将处理后的护理项目计划列表保存到数据库
         */

        boolean flag = iNursingProjectPlanService.saveBatch(projectPlans);
        deleteCache();
        return flag ? 1 : 0;
    }

    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }


    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan (NursingPlanDto nursingPlanDto)
    {
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlanDto.getId());
            });
            iNursingProjectPlanService.saveBatch(projectPlans);
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        boolean flag = updateById(nursingPlan);
        deleteCache();
        return flag ? 1 : 0;
    }
    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById (Long id)
    {
        iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        boolean flag = removeById(id);
        deleteCache();
        return flag ? 1 : 0;
    }
}
